<template>
  <div class="insurance-agent-container ">
    <div class="top-box">
      <el-card class="card my-info">
        <div class="banner-box">
          <div class="title-box">
            <div class="title">广东电信住院医疗险</div>
            <div class="tip">能<span class="red">保一辈子</span>的医疗险</div>
          </div>
          <div class="number-box">
            <div class="number1">报销额度 <span class="red">600万</span></div>
            <div class="number2">
              <div class="icon" />保险热销总榜第一
            </div>
          </div>
        </div>
        <div class="info-box">
          <div class="info-item my-base">
            <div class="avatar" />
            <div class="title">{{ nickname }}</div>
            <div class="link">已有保单 {{ countPersonaInsuranceData[0] && countPersonaInsuranceData[0].value }} ></div>
          </div>
          <div class="info-item my-claims">
            <div class="title">我的理赔</div>
            <div class="link">在线理赔测算 ></div>
          </div>
          <div class="info-item my-info">
            <div class="title"></div>
            <div class="link">完善信息 ></div>
          </div>
        </div>
      </el-card>
      <el-card class="card insurance-selected">
        <div class="handle-box">
          <div class="user-list">
            <span class="user">{{ nickname }}</span>
          </div>
          <div class="family">
            <svg-icon icon-class="add" />为家人定制
          </div>
        </div>
        <div class="left-box">
          <div class="insurance-item"
            :class="countPersonaInsuranceData[1] && countPersonaInsuranceData[1].value && 'active'"
            @click="showInsuranceEditDialog(countPersonaInsuranceData[1] && countPersonaInsuranceData[1].name)">
            <div class="text-box">
              <div class="label">
                <span>
                  {{ countPersonaInsuranceData[1] && countPersonaInsuranceData[1].name }}
                </span>
              </div>
              <div class="value">
                <span v-if="countPersonaInsuranceData[1] && countPersonaInsuranceData[1].value">
                  {{ `保障${countPersonaInsuranceData[1].value}份` }}
                </span>
                <span v-else>未保障</span>
              </div>
            </div>
            <div class="icon" />
          </div>
          <div class="insurance-item"
            :class="countPersonaInsuranceData[3] && countPersonaInsuranceData[3].value && 'active'"
            @click="showInsuranceEditDialog(countPersonaInsuranceData[3] && countPersonaInsuranceData[3].name)">
            <div class="text-box">
              <div class="label">
                <span>
                  {{ countPersonaInsuranceData[3] && countPersonaInsuranceData[3].name }}
                </span>
              </div>
              <div class="value">
                <span v-if="countPersonaInsuranceData[3] && countPersonaInsuranceData[3].value">
                  {{ `保障${countPersonaInsuranceData[3].value}份` }}
                </span>
                <span v-else>未保障</span>
              </div>
            </div>
            <div class="icon" />
          </div>
        </div>
        <div class="center-box">
          <div class="title">保障程度</div>
          <div class="num">已有保障{{ countPersonaInsuranceData[0] && countPersonaInsuranceData[0].value }}份</div>
          <div class="add">添加保险</div>
        </div>
        <div class="right-box">
          <div class="insurance-item"
            :class="countPersonaInsuranceData[2] && countPersonaInsuranceData[2].value && 'active'"
            @click="showInsuranceEditDialog(countPersonaInsuranceData[2] && countPersonaInsuranceData[2].name)">
            <div class="icon" />
            <div class="text-box">
              <div class="label">
                <span>
                  {{ countPersonaInsuranceData[2] && countPersonaInsuranceData[2].name }}
                </span>
              </div>
              <div class="value">
                <span v-if="countPersonaInsuranceData[2] && countPersonaInsuranceData[2].value">
                  {{ `保障${countPersonaInsuranceData[2].value}份` }}
                </span>
                <span v-else>未保障</span>
              </div>
            </div>
          </div>
          <div class="insurance-item"
            :class="countPersonaInsuranceData[4] && countPersonaInsuranceData[4].value && 'active'"
            @click="showInsuranceEditDialog(countPersonaInsuranceData[4] && countPersonaInsuranceData[4].name)">
            <div class="icon" />
            <div class="text-box">
              <div class="label">
                <span>
                  {{ countPersonaInsuranceData[4] && countPersonaInsuranceData[4].name }}
                </span>
              </div>
              <div class="value">
                <span v-if="countPersonaInsuranceData[4] && countPersonaInsuranceData[4].value">
                  {{ `保障${countPersonaInsuranceData[4].value}份` }}
                </span>
                <span v-else>未保障</span>
              </div>
            </div>
          </div>
        </div>
      </el-card>
    </div>
    <div class="bottom-box">
      <el-card class="card insurance-recommend">
        <div class="card-title">
          <div class="title">保险推荐</div>
          <svg-icon icon-class="back" @click="backForm" />
        </div>
        <div class="content-box">
          <div class="nav-box">
            <div class="type-box">
              <div class="type-item" :class="insuranceType_active == item.label && 'active'"
                v-for="item in insuranceTypeList" @click="selectInsuranceType(item)">
                {{ item.label }}
              </div>
            </div>
            <div class="search-box">
              <el-input v-model="searchForm.name" placeholder="搜索" suffix-icon="el-icon-search" />
            </div>
          </div>
          <div class="result-box" v-loading="dataLoading" :class="insuranceType_active == '推荐' && 'recommend'">
            <div class="reason-box" v-if="insuranceType_active !== '推荐'">
              <div class="title">{{ '为什么需要医疗险?' }}</div>
              <div class="tip">{{ '广东东住院成本较高，疾病出现年轻化趋势，一日发生，可能没法负担大额医疗费?' }}</div>
              <div class="content">
                {{ '生病后，通常有院内治疗、康复护理和收入损失三笔支出。住院医疗险主要用于报销院内治疗费用。' }}
                <img :src="require('@/views/AiBenefitAssociation/RecommendedInsurance/img/reason-img.png')" alt="">
              </div>
            </div>
            <svg-icon icon-class="right-arrow-fill" v-if="insuranceType_active !== '推荐'" />
            <div class="result-list">
              <div class="result-list-title" v-if="insuranceType_active !== '推荐'">已精准匹配合适产品</div>
              <div class="insurance-item" v-for="item in insuranceListData" @click="showInsuranceDetailDialog(item)">
                <div class="base-info">
                  <div class="img-box"></div>
                  <div class="text-box">
                    <div class="name">{{ item.insurance_name }}</div>
                    <div class="tip"></div>
                    <div class="price">{{ item.insured_amount }} 元起</div>
                  </div>
                </div>
                <div class="more-info">
                  <div class="info-item">
                    <div class="value">{{ item.protection_amount }}万</div>
                    <div class="label">保额</div>
                  </div>
                  <div class="info-item">
                    <div class="value">{{ item.coverage }}</div>
                    <div class="label">保障范围</div>
                  </div>
                  <div class="info-item">
                    <div class="value">{{ item.guarantee_object }}</div>
                    <div class="label">保障对象</div>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>
      </el-card>
    </div>
    <component :is="dialogName" :visible.sync="dialogVisible" :params="dialogParams" :width="dialogWidth"
      :height="dialogHeight" @hide="getData" />
  </div>
</template>

<script>
import { mapGetters } from 'vuex';
import * as api from '../api.js';
// 批量局部注册组件
const componentsObj = {};
const req = require.context('./dialog', true, /\.vue$/);
req.keys().forEach((element, index) => {
  let name = element.replace(/(.*\/)|(\.vue)/gi, '');
  const config = req(element);
  componentsObj[name] = config.default || config;
});

export default {
  name: 'InsuranceRecommend',
  components: componentsObj,
  props: {
    formData: {
      type: Object,
      default: () => {
        return {};
      },
    },
  },
  data() {
    return {
      dataLoading: false,
      insuranceTypeList: [{ label: '推荐' }, { label: '住院险' }, { label: '意外险' }, { label: '百万医保' }],
      insuranceType_active: '推荐',
      searchForm: { name: '' },
      insuranceListData: [],
      countPersonaInsuranceData: {},
      dialogName: '',
      dialogVisible: false,
      dialogParams: {},
      dialogWidth: '',
      dialogHeight: '',
    };
  },
  created() {
    this.getData();
  },
  computed: {
    ...mapGetters(['nickname']),
  },
  methods: {
    backForm() {
      this.$emit('back');
    },
    getData() {
      this.getInsuranceList();
      this.getCountPersonaInsurance();
    },
    getInsuranceList() {
      this.dataLoading = true;
      api.getList(this.formData).then((res) => {
        // res.rows = [...res.rows, ...res.rows, ...res.rows, ...res.rows, ...res.rows];
        res.rows = res.rows;
        if (this.insuranceType_active != '推荐') {
          res.rows = res.rows.filter((item) => item.insurance_type == this.insuranceType_active);
        }
        this.insuranceListData = res.rows;
        this.dataLoading = false;
      });
    },
    getCountPersonaInsurance() {
      api.getCountPersonaInsuranceByInsuranceType().then(({ data }) => {
        this.countPersonaInsuranceData = data;
      });
    },
    selectInsuranceType(type) {
      this.insuranceType_active = type.label;
      this.getInsuranceList();
    },
    showInsuranceDetailDialog(item) {
      if (item.details) {
        this.dialogName = 'InsuranceDetailDialog';
        this.dialogWidth = '600px';
        this.dialogHeight = '900px';
        this.dialogVisible = true;
        const details = JSON.parse(item.details);
        delete details['阅读提示'];
        this.dialogParams = { details: details };
      }
    },
    showInsuranceEditDialog(type) {
      this.dialogName = 'InsuranceListDialog';
      this.dialogWidth = '700px';
      this.dialogHeight = '500px';
      this.dialogVisible = true;
      this.dialogParams = { type: type };
    },
  },
};
</script>

<style lang="scss" scoped>
::v-deep .card {
  border-radius: 29px;

  .el-card__body {
    padding: 0;
    height: 100%;
    width: 100%;
  }
}

.top-box {
  // flex: 1;
  height: 350px;
  display: flex;
}

.card {
  &.my-info {
    flex: 1.8;

    .banner-box {
      width: 100%;
      height: 65%;
      padding-top: 22px;
      color: #fff;
      background-color: #79d4f7;
      background-image: url('../img/my-info-banner.png');
      background-repeat: no-repeat;
      background-position-x: right;
      background-size: auto 100%;
      display: flex;

      .red {
        color: #ff5252;
      }

      .title-box {
        margin: 0 40px;

        .title {
          font-size: 3rem;
          font-family: 'YouSheBiaoTiHei';
        }

        .tip {
          font-size: 1.5rem;
        }
      }

      .number-box {
        font-size: 1.125rem;

        .number1 {
          margin: 10px 0;
        }

        .number2 {
          display: flex;
          align-items: center;

          .icon {
            width: 24px;
            height: 24px;
            margin-right: 5px;
            background-image: url('../img/top-icon.png');
            background-repeat: no-repeat;
            background-size: 100% 100%;
          }
        }
      }
    }

    .info-box {
      width: 100%;
      height: 35%;
      padding: 20px 0;
      display: flex;

      .info-item {
        flex: 1;
        padding: 0 50px;
        border-right: 1px solid #d8d8d8;
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: end;

        &:last-child {
          border: none;
        }

        &:first-child {
          position: relative;
          flex: 1.5;
          align-items: end;
        }

        .avatar {
          position: absolute;
          left: 100px;
          width: 150px;
          height: 150px;
          background-image: url('../img/avatar.png');
          background-repeat: no-repeat;
          background-size: 100% 100%;
        }

        .title {
          font-size: 2rem;
        }

        .link {
          font-size: 1.25rem;
          color: #9a9a9a;
        }
      }
    }
  }

  &.insurance-selected {
    flex: 1;
    margin-left: 20px;
    background-image: url('../img/insurance-selected-BG.png');
    background-repeat: no-repeat;
    background-size: 280px 280px;
    background-position: center;

    ::v-deep .el-card__body {
      display: flex;
      // position:absolute;
      position: relative;
    }

    .handle-box {
      position: absolute;
      top: 10px;
      width: 100%;
      padding: 0 30px;
      display: flex;
      justify-content: space-between;
      align-items: center;
      .user-list {
        .user {
          color: #165dff;
          padding: 5px 0;
          border-bottom: 2px solid #165dff;
        }
      }
      .family {
        display: flex;
        align-items: center;
        color: #c8c8c8;
        cursor: pointer;
        .svg-icon.add {
          font-size: 20px;
        }
      }
    }

    .center-box {
      flex-shrink: 0;
      display: flex;
      flex-direction: column;
      align-items: center;
      justify-content: center;
      margin: 0px 30px;

      .title {
        font-size: 20px;
        color: #b9b9b9;
      }

      .num {
        font-size: 20px;
      }

      .add {
        font-size: 16px;
        color: #b9b9b9;
      }
    }

    .left-box,
    .right-box {
      flex: 1;
      display: flex;
      flex-direction: column;
      justify-content: space-around;

      .insurance-item {
        display: flex;
        align-items: center;
        font-size: 20px;
        cursor: pointer;

        &.active {
          .value {
            color: #165dff;
          }

          .icon {
            background-image: url('../img/insurance-partial-icon.png');
          }
        }

        .value {
          color: #d6d6d6;
        }

        .icon {
          width: 60px;
          height: 60px;
          margin: 0 10px;
          background-image: url('../img/insurance-not-icon.png');
          background-repeat: no-repeat;
          background-size: 100% 100%;
        }
      }
    }

    .left-box {
      align-items: end;
    }

    .right-box {
      align-items: start;
    }
  }
}

.bottom-box {
  height: calc(100vh - 84px - 410px);
  margin-top: 20px;
  display: flex;
}

.card.insurance-recommend {
  flex: 1;

  ::v-deep .el-card__body {
    padding: 20px;
  }

  .card-title {
    flex-shrink: 0;
    margin-bottom: 20px;
    display: flex;
    justify-content: space-between;
    align-items: center;

    .title {
      font-size: 30px;
    }

    .svg-icon.back {
      font-size: 32px;
      color: #2f80ed;
      cursor: pointer;
    }
  }

  .content-box {
    height: calc(100% - 70px);
    width: 100%;

    .nav-box {
      width: 100%;
      display: flex;
      justify-content: space-between;
      align-items: center;
      margin-bottom: 20px;

      .type-box {
        display: flex;

        .type-item {
          font-size: 20px;
          border-radius: 32px;
          cursor: pointer;
          padding: 8px 20px;
          margin-right: 15px;

          &:last-child {
            margin-right: 0px;
          }

          &:hover {
            background-color: #f2f3f8;
          }

          &.active {
            background-color: #f2f3f8;
            color: #165dff;
          }
        }
      }

      .search-box {
        width: 25%;

        ::v-deep .el-input--medium {
          font-size: 16px;

          .el-input__inner {
            height: 40px;
            line-height: 40px;
            background-color: #f2f3f5;
            border: none;
          }
        }
      }
    }

    .result-box {
      height: calc(100% - 59.75px);
      width: 100%;
      display: flex;
      justify-content: space-between;
      // overflow: auto;

      .reason-box {
        padding: 30px;
        border-radius: 16px;
        height: 100%;
        width: 40%;
        background: #f8f8f8;
        display: flex;
        flex-direction: column;

        .title {
          font-size: 32px;
          margin-bottom: 15px;
        }

        .tip {
          font-size: 26px;
          color: #8e8e8e;
          margin-bottom: 25px;
        }

        .content {
          flex: 1;
          padding: 30px;
          border-radius: 16px;
          background: #e4f8ff;
          font-size: 26px;
          color: #2f80ed;
          display: flex;

          img {
            flex: 0.5;
            margin-left: 30px;
            // width: 200px;
            // height: auto;
            // height: 128px;
          }
        }
      }

      .svg-icon.right-arrow-fill {
        font-size: 24px;
        color: #2f80ed;
      }

      .result-list {
        width: 50%;
        height: 100%;
        overflow: auto;

        // position: relative;
        // padding-top: 30px;
        .result-list-title {
          // position: absolute;
          font-size: 26px;
          margin-bottom: 20px;
        }
      }

      &.recommend {
        .result-list {
          width: 100%;
          // padding-top: 0px;
        }

        .insurance-item {
          width: 24%;

          &:nth-child(4n) {
            margin-right: 0px;
          }

          &:nth-child(2n-1) {
            margin-right: 20px;
          }
        }
      }

      .insurance-item {
        display: inline-block;
        width: 47.5%;
        border-radius: 5px;
        border: 1px solid #e5e8ef;
        padding: 15px;
        margin-bottom: 20px;
        margin-right: 20px;
        cursor: pointer;

        &:nth-child(2n-1) {
          margin-right: 0px;
        }

        .base-info {
          display: flex;
          margin-bottom: 15px;

          .img-box {
            flex-shrink: 0;
            width: 120px;
            height: 120px;
            margin-right: 15px;
            background-image: url('../img/insurance-img.png');
            background-repeat: no-repeat;
            background-size: 100% 100%;
          }

          .text-box {
            flex: 1;
            display: flex;
            flex-direction: column;
            justify-content: space-between;

            .name {
              font-size: 22px;
            }

            .price {
              font-size: 22px;
              color: #ff0000;
            }
          }
        }

        .more-info {
          // flex: 1;
          display: flex;
          width: 100%;

          .info-item {
            flex: 1;
            text-align: center;
            font-size: 20px;
            border-right: 1px solid #d8d8d8;

            &:last-child {
              border: none;
            }

            .label {
              color: #c8c8c8;
            }
          }
        }
      }
    }
  }
}
</style>
